home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / TIFFJPEG.ZIP / TIFFTechNote2.txt
Text File  |  1995-03-17  |  36KB  |  706 lines

  1. DRAFT TIFF Technical Note #2                17-Mar-95
  2. ============================
  3.  
  4. This Technical Note describes serious problems that have been found in
  5. TIFF 6.0's design for embedding JPEG-compressed data in TIFF (Section 22
  6. of the TIFF 6.0 spec of 3 June 1992).  A replacement TIFF/JPEG
  7. specification is given.  Some corrections to Section 21 are also given.
  8.  
  9. To permit TIFF implementations to continue to read existing files, the 6.0
  10. JPEG fields and tag values will remain reserved indefinitely.  However,
  11. TIFF writers are strongly discouraged from using the 6.0 JPEG design.  It
  12. is expected that the next full release of the TIFF specification will not
  13. describe the old design at all, except to note that certain tag numbers
  14. are reserved.  The existing Section 22 will be replaced by the
  15. specification text given in the second part of this Tech Note.
  16.  
  17.  
  18. Problems in TIFF 6.0 JPEG
  19. =========================
  20.  
  21. Abandoning a published spec is not a step to be taken lightly.  This
  22. section summarizes the reasons that have forced this decision.
  23. TIFF 6.0's JPEG design suffers from design errors and limitations,
  24. ambiguities, and unnecessary complexity.
  25.  
  26.  
  27. Design errors and limitations
  28. -----------------------------
  29.  
  30. The fundamental design error in the existing Section 22 is that JPEG's
  31. various tables and parameters are broken out as separate fields which the
  32. TIFF control logic must manage.  This is bad software engineering: that
  33. information should be treated as private to the JPEG codec
  34. (compressor/decompressor).  Worse, the fields themselves are specified
  35. without sufficient thought for future extension and without regard to
  36. well-established TIFF conventions.  Here are some of the significant
  37. problems:
  38.  
  39. * The JPEGxxTable fields do not store the table data directly in the
  40. IFD/field structure; rather, the fields hold pointers to information
  41. elsewhere in the file.  This requires special-purpose code to be added to
  42. *every* TIFF-manipulating application, whether it needs to decode JPEG
  43. image data or not.  Even a trivial TIFF editor, for example a program to
  44. add an ImageDescription field to a TIFF file, must be explicitly aware of
  45. the internal structure of the JPEG-related tables, or else it will probably
  46. break the file.  Every other auxiliary field in the TIFF spec contains
  47. data, not pointers, and can be copied or relocated by standard code that
  48. doesn't know anything about the particular field.  This is a crucial
  49. property of the TIFF format that must not be given up.
  50.  
  51. * To manipulate these fields, the TIFF control logic is required to know a
  52. great deal about JPEG details, for example such arcana as how to compute
  53. the length of a Huffman code table --- the length is not supplied in the
  54. field structure and can only be found by inspecting the table contents.
  55. This is again a violation of good software practice.  Moreover, it will
  56. prevent easy adoption of future JPEG extensions that might change these
  57. low-level details.
  58.  
  59. * The design neglects the fact that baseline JPEG codecs support only two
  60. sets of Huffman tables: it specifies a separate table for each color
  61. component.  This implies that encoders must waste space (by storing
  62. duplicate Huffman tables) or else violate the well-founded TIFF convention
  63. that prohibits duplicate pointers.  Furthermore, baseline decoders must
  64. test to find out which tables are identical, a waste of time and code
  65. space.
  66.  
  67. * The JPEGInterchangeFormat field also violates TIFF's proscription against
  68. duplicate pointers: the normal strip/tile pointers are expected to point
  69. into the larger data area pointed to by JPEGInterchangeFormat.  All TIFF
  70. editing applications must be specifically aware of this relationship, since
  71. they must maintain it or else delete the JPEGInterchangeFormat field.  The
  72. JPEGxxTables fields are also likely to point into the JPEGInterchangeFormat
  73. area, creating additional pointer relationships that must be maintained.
  74.  
  75. * The JPEGQTables field is fixed at a byte per table entry; there is no
  76. way to support 16-bit quantization values.  This is a serious impediment
  77. to extending TIFF to use 12-bit JPEG.
  78.  
  79. * The 6.0 design cannot support using different quantization tables in
  80. different strips/tiles of an image (so as to encode some areas at higher
  81. quality than others).  Furthermore, since quantization tables are tied
  82. one-for-one to color components, the design cannot support table switching
  83. options that are likely to be added in future JPEG revisions.
  84.  
  85.  
  86. Ambiguities
  87. -----------
  88.  
  89. Several incompatible interpretations are possible for 6.0's treatment of
  90. JPEG restart markers:
  91.  
  92.   * It is unclear whether restart markers must be omitted at TIFF segment
  93.     (strip/tile) boundaries, or whether they are optional.
  94.  
  95.   * It is unclear whether the segment size is required to be chosen as
  96.     a multiple of the specified restart interval (if any); perhaps the
  97.     JPEG codec is supposed to be reset at each segment boundary as if
  98.     there were a restart marker there, even if the boundary does not fall
  99.     at a multiple of the nominal restart interval.
  100.  
  101.   * The spec fails to address the question of restart marker numbering:
  102.     do the numbers begin again within each segment, or not?
  103.  
  104. That last point is particularly nasty.  If we make numbering begin again
  105. within each segment, we give up the ability to impose a TIFF strip/tile
  106. structure on an existing JPEG datastream with restarts (which was clearly a
  107. goal of Section 22's authors).  But the other choice interferes with random
  108. access to the image segments: a reader must compute the first restart
  109. number to be expected within a segment, and must have a way to reset its
  110. JPEG decoder to expect a nonzero restart number first.  This may not even
  111. be possible with some JPEG chips.
  112.  
  113. The tile height restriction found on page 104 contradicts Section 15's
  114. general description of tiles.  For an image that is not vertically
  115. downsampled, page 104 specifies a tile height of one MCU or 8 pixels; but
  116. Section 15 requires tiles to be a multiple of 16 pixels high.
  117.  
  118. This Tech Note does not attempt to resolve these ambiguities, so
  119. implementations that follow the 6.0 design should be aware that
  120. inter-application compatibility problems are likely to arise.
  121.  
  122.  
  123. Unnecessary complexity
  124. ----------------------
  125.  
  126. The 6.0 design creates problems for implementations that need to keep the
  127. JPEG codec separate from the TIFF control logic --- for example, consider
  128. using a JPEG chip that was not designed specifically for TIFF.  JPEG codecs
  129. generally want to produce or consume a standard ISO JPEG datastream, not
  130. just raw compressed data.  (If they were to handle raw data, a separate
  131. out-of-band mechanism would be needed to load tables into the codec.)
  132. With such a codec, the TIFF control logic must parse JPEG markers emitted
  133. by the codec to create the TIFF table fields (when writing) or synthesize
  134. JPEG markers from the TIFF fields to feed the codec (when reading).  This
  135. means that the control logic must know a great deal more about JPEG details
  136. than we would like.  The parsing and reconstruction of the markers also
  137. represents a fair amount of unnecessary work.
  138.  
  139. Quite a few implementors have proposed writing "TIFF/JPEG" files in which
  140. a standard JPEG datastream is simply dumped into the file and pointed to
  141. by JPEGInterchangeFormat.  To avoid parsing the JPEG datastream, they
  142. suggest not writing the JPEG auxiliary fields (JPEGxxTables etc) nor even
  143. the basic TIFF strip/tile data pointers.  This approach is incompatible
  144. with implementations that handle the full TIFF 6.0 JPEG design, since they
  145. will expect to find strip/tile pointers and auxiliary fields.  Indeed this
  146. is arguably not TIFF at all, since *all* TIFF-reading applications expect
  147. to find strip or tile pointers.  A subset implementation that is not
  148. upward-compatible with the full spec is clearly unacceptable.  However,
  149. the frequency with which this idea has come up makes it clear that
  150. implementors find the existing Section 22 too complex.
  151.  
  152.  
  153. Overview of the solution
  154. ========================
  155.  
  156. To solve these problems, we adopt a new design for embedding
  157. JPEG-compressed data in TIFF files.  The new design uses only complete,
  158. uninterpreted ISO JPEG datastreams, so it should be much more forgiving of
  159. extensions to the ISO standard.  It should also be far easier to implement
  160. using unmodified JPEG codecs.
  161.  
  162. To reduce overhead in multi-segment TIFF files, we allow JPEG overhead
  163. tables to be stored just once in a JPEGTables auxiliary field.  This
  164. feature does not violate the integrity of the JPEG datastreams, because it
  165. uses the notions of "tables-only datastreams" and "abbreviated image
  166. datastreams" as defined by the ISO standard.
  167.  
  168. To prevent confusion with the old design, the new design is given a new
  169. Compression tag value, Compression=7.  Readers that need to handle
  170. existing 6.0 JPEG files may read both old and new files, using whatever
  171. interpretation of the 6.0 spec they did before.  Compression tag value 6
  172. and the field tag numbers defined by 6.0 section 22 will remain reserved
  173. indefinitely, even though detailed descriptions of them will be dropped
  174. from future editions of the TIFF specification.
  175.  
  176.  
  177. Replacement TIFF/JPEG specification
  178. ===================================
  179.  
  180. [This section of the Tech Note is expected to replace Section 22 in the
  181. next release of the TIFF specification.]
  182.  
  183. This section describes TIFF compression scheme 7, a high-performance
  184. compression method for continuous-tone images.
  185.  
  186. Introduction
  187. ------------
  188.  
  189. This TIFF compression method uses the international standard for image
  190. compression ISO/IEC 10918-1, usually known as "JPEG" (after the original
  191. name of the standards committee, Joint Photographic Experts Group).  JPEG
  192. is a joint ISO/CCITT standard for compression of continuous-tone images.
  193.  
  194. The JPEG committee decided that because of the broad scope of the standard,
  195. no one algorithmic procedure was able to satisfy the requirements of all
  196. applications.  Instead, the JPEG standard became a "toolkit" of multiple
  197. algorithms and optional capabilities.  Individual applications may select
  198. a subset of the JPEG standard that meets their requirements.
  199.  
  200. The most important distinction among the JPEG processes is between lossy
  201. and lossless compression.  Lossy compression methods provide high
  202. compression but allow only approximate reconstruction of the original
  203. image.  JPEG's lossy processes allow the encoder to trade off compressed
  204. file size against reconstruction fidelity over a wide range.  Typically,
  205. 10:1 or more compression of full-color data can be obtained while keeping
  206. the reconstructed image visually indistinguishable from the original.  Much
  207. higher compression ratios are possible if a low-quality reconstructed image
  208. is acceptable.  Lossless compression provides exact reconstruction of the
  209. source data, but the achievable compression ratio is much lower than for
  210. the lossy processes; JPEG's rather simple lossless process typically
  211. achieves around 2:1 compression of full-color data.
  212.  
  213. The most widely implemented JPEG subset is the "baseline" JPEG process.
  214. This provides lossy compression of 8-bit-per-channel data.  Optional
  215. extensions include 12-bit-per-channel data, arithmetic entropy coding for
  216. better compression, and progressive/hierarchical representations.  The
  217. lossless process is an independent algorithm that has little in
  218. common with the lossy processes.
  219.  
  220. It should be noted that the optional arithmetic-coding extension is subject
  221. to several US and Japanese patents.  To avoid patent problems, use of
  222. arithmetic coding processes in TIFF files intended for inter-application
  223. interchange is discouraged.
  224.  
  225. All of the JPEG processes are useful only for "continuous tone" data,
  226. in which the difference between adjacent pixel values is usually small.
  227. Low-bit-depth source data is not appropriate for JPEG compression, nor
  228. are palette-color images good candidates.  The JPEG processes work well
  229. on grayscale and full-color data.
  230.  
  231. Describing the JPEG compression algorithms in sufficient detail to permit
  232. implementation would require more space than we have here.  Instead, we
  233. refer the reader to the References section.
  234.  
  235.  
  236. What data is being compressed?
  237. ------------------------------
  238.  
  239. In lossy JPEG compression, it is customary to convert color source data
  240. to YCbCr and then downsample it before JPEG compression.  This gives
  241. 2:1 data compression with hardly any visible image degradation, and it
  242. permits additional space savings within the JPEG compression step proper.
  243. However, these steps are not considered part of the ISO JPEG standard.
  244. The ISO standard is "color blind": it accepts data in any color space.
  245.  
  246. For TIFF purposes, the JPEG compression tag is considered to represent the
  247. ISO JPEG compression standard only.  The ISO standard is applied to the
  248. same data that would be stored in the TIFF file if no compression were
  249. used.  Therefore, if color conversion or downsampling are used, they must
  250. be reflected in the regular TIFF fields; these steps are not considered to
  251. be implicit in the JPEG compression tag value.  PhotometricInterpretation
  252. and related fields shall describe the color space actually stored in the
  253. file.  With the TIFF 6.0 field definitions, downsampling is permissible
  254. only for YCbCr data, and it must correspond to the YCbCrSubSampling field.
  255. (Note that the default value for this field is not 1,1; so the default for
  256. YCbCr is to apply downsampling!)  It is likely that future versions of TIFF
  257. will provide additional PhotometricInterpretation values and a more general
  258. way of defining subsampling, so as to allow more flexibility in
  259. JPEG-compressed files.  But that issue is not addressed in this Tech Note.
  260.  
  261. Implementors should note that many popular JPEG codecs
  262. (compressor/decompressors) provide automatic color conversion and
  263. downsampling, so that the application may supply full-size RGB data which
  264. is nonetheless converted to downsampled YCbCr.  This is an implementation
  265. convenience which does not excuse the TIFF control layer from its
  266. responsibility to know what is really going on.  The
  267. PhotometricInterpretation and subsampling fields written to the file must
  268. describe what is actually in the file.
  269.  
  270. A JPEG-compressed TIFF file will typically have PhotometricInterpretation =
  271. YCbCr and YCbCrSubSampling = [2,1] or [2,2], unless the source data was
  272. grayscale or CMYK.
  273.  
  274.  
  275. Basic representation of JPEG-compressed images
  276. ----------------------------------------------
  277.  
  278. JPEG compression works in either strip-based or tile-based TIFF files.
  279. Rather than repeating "strip or tile" constantly, we will use the term
  280. "segment" to mean either a strip or a tile.
  281.  
  282. When the Compression field has the value 7, each image segment contains
  283. a complete JPEG datastream which is valid according to the ISO JPEG
  284. standard (ISO/IEC 10918-1).  Any sequential JPEG process can be used,
  285. including lossless JPEG, but progressive and hierarchical processes are not
  286. supported.  Since JPEG is useful only for continuous-tone images, the
  287. PhotometricInterpretation of the image shall not be 3 (palette color) nor
  288. 4 (transparency mask).  The bit depth of the data is also restricted as
  289. specified below.
  290.  
  291. Each image segment in a JPEG-compressed TIFF file shall contain a valid
  292. JPEG datastream according to the ISO JPEG standard's rules for
  293. interchange-format or abbreviated-image-format data.  The datastream shall
  294. contain a single JPEG frame storing that segment of the image.  The
  295. required JPEG markers within a segment are:
  296.     SOI    (must appear at very beginning of segment)
  297.     SOFn
  298.     SOS    (one for each scan, if there is more than one scan)
  299.     EOI    (must appear at very end of segment)
  300. The actual compressed data follows SOS; it may contain RSTn markers if DRI
  301. is used.
  302.  
  303. Additional JPEG "tables and miscellaneous" markers may appear between SOI
  304. and SOFn, between SOFn and SOS, and before each subsequent SOS if there is
  305. more than one scan.  These markers include:
  306.     DQT
  307.     DHT
  308.     DAC    (not to appear unless arithmetic coding is used)
  309.     DRI
  310.     APPn    (shall be ignored by TIFF readers)
  311.     COM    (shall be ignored by TIFF readers)
  312. DNL markers shall not be used in TIFF files.  Readers should abort if any
  313. other marker type is found, especially the JPEG reserved markers;
  314. occurrence of such a marker is likely to indicate a JPEG extension.
  315.  
  316. The tables/miscellaneous markers may appear in any order.  Readers are
  317. cautioned that although the SOFn marker refers to DQT tables, JPEG does not
  318. require those tables to precede the SOFn, only the SOS.  Missing-table
  319. checks should be made when SOS is reached.
  320.  
  321. If no JPEGTables field is used, then each image segment shall be a complete
  322. JPEG interchange datastream.  Each segment must define all the tables it
  323. references.  To allow readers to decode segments in any order, no segment
  324. may rely on tables being carried over from a previous segment.
  325.  
  326. When a JPEGTables field is used, image segments may omit tables that have
  327. been specified in the JPEGTables field.  Further details appear below.
  328.  
  329. The SOFn marker shall be of type SOF0 for strict baseline JPEG data, of
  330. type SOF1 for non-baseline lossy JPEG data, or of type SOF3 for lossless
  331. JPEG data.  (SOF9 or SOF11 would be used for arithmetic coding.)  All
  332. segments of a JPEG-compressed TIFF image shall use the same JPEG
  333. compression process, in particular the same SOFn type.
  334.  
  335. The data precision field of the SOFn marker shall agree with the TIFF
  336. BitsPerSample field.  (Note that when PlanarConfiguration=1, this implies
  337. that all components must have the same BitsPerSample value; when
  338. PlanarConfiguration=2, different components could have different bit
  339. depths.)  For SOF0 only precision 8 is permitted; for SOF1, precision 8 or
  340. 12 is permitted; for SOF3, precisions 2 to 16 are permitted.
  341.  
  342. The image dimensions given in the SOFn marker shall agree with the logical
  343. dimensions of that particular strip or tile.  For strip images, the SOFn
  344. image width shall equal ImageWidth and the height shall equal RowsPerStrip,
  345. except in the last strip; its SOFn height shall equal the number of rows
  346. remaining in the ImageLength.  (In other words, no padding data is counted
  347. in the SOFn dimensions.)  For tile images, each SOFn shall have width
  348. TileWidth and height TileHeight; adding and removing any padding needed in
  349. the edge tiles is the concern of some higher level of the TIFF software.
  350. (The dimensional rules are slightly different when PlanarConfiguration=2,
  351. as described below.)
  352.  
  353. The ISO JPEG standard only permits images up to 65535 pixels in width or
  354. height, due to 2-byte fields in the SOFn markers.  In TIFF, this limits
  355. the size of an individual JPEG-compressed strip or tile, but the total
  356. image size can be greater.
  357.  
  358. The number of components in the JPEG datastream shall equal SamplesPerPixel
  359. for PlanarConfiguration=1, and shall be 1 for PlanarConfiguration=2.  The
  360. components shall be stored in the same order as they are described at the
  361. TIFF field level.  (This applies both to their order in the SOFn marker,
  362. and to the order in which they are scanned if multiple JPEG scans are
  363. used.)  The component ID bytes are arbitrary so long as each component
  364. within an image segment is given a distinct ID.  To avoid any possible
  365. confusion, we require that all segments of a TIFF image use the same ID
  366. code for a given component.
  367.  
  368. In PlanarConfiguration 1, the sampling factors given in SOFn markers shall
  369. agree with the sampling factors defined by the related TIFF fields (or with
  370. the default values that are specified in the absence of those fields).
  371.  
  372. When DCT-based JPEG is used in a strip TIFF file, RowsPerStrip is required
  373. to be a multiple of 8 times the largest vertical sampling factor, i.e., a
  374. multiple of the height of an interleaved MCU.  (For simplicity of
  375. specification, we require this even if the data is not actually
  376. interleaved.)  For example, if YCbCrSubSampling = [2,2] then RowsPerStrip
  377. must be a multiple of 16.  An exception to this rule is made for
  378. single-strip images (RowsPerStrip >= ImageLength): the exact value of
  379. RowsPerStrip is unimportant in that case.  This rule ensures that no data
  380. padding is needed at the bottom of a strip, except perhaps the last strip.
  381. Any padding required at the right edge of the image, or at the bottom of
  382. the last strip, is expected to occur internally to the JPEG codec.
  383.  
  384. When DCT-based JPEG is used in a tiled TIFF file, TileLength is required
  385. to be a multiple of 8 times the largest vertical sampling factor, i.e.,
  386. a multiple of the height of an interleaved MCU; and TileWidth is required
  387. to be a multiple of 8 times the largest horizontal sampling factor, i.e.,
  388. a multiple of the width of an interleaved MCU.  (For simplicity of
  389. specification, we require this even if the data is not actually
  390. interleaved.)  All edge padding required will therefore occur in the course
  391. of normal TIFF tile padding; it is not special to JPEG.
  392.  
  393. Lossless JPEG does not impose these constraints on strip and tile sizes,
  394. since it is not DCT-based.
  395.  
  396. Note that within JPEG datastreams, multibyte values appear in the MSB-first
  397. order specified by the JPEG standard, regardless of the byte ordering of
  398. the surrounding TIFF file.
  399.  
  400.  
  401. JPEGTables field
  402. ----------------
  403.  
  404. The only auxiliary TIFF field added for Compression=7 is the optional
  405. JPEGTables field.  The purpose of JPEGTables is to predefine JPEG
  406. quantization and/or Huffman tables for subsequent use by JPEG image
  407. segments.  When this is done, these rather bulky tables need not be
  408. duplicated in each segment, thus saving space and processing time.
  409. JPEGTables may be used even in a single-segment file, although there is no
  410. space savings in that case.
  411.  
  412. JPEGTables:
  413.     Tag = 347 (15B.H)
  414.     Type = UNDEFINED
  415.     N = number of bytes in tables datastream, typically a few hundred
  416. JPEGTables provides default JPEG quantization and/or Huffman tables which
  417. are used whenever a segment datastream does not contain its own tables, as
  418. specified below.
  419.  
  420. Notice that the JPEGTables field is required to have type code UNDEFINED,
  421. not type code BYTE.  This is to cue readers that expanding individual bytes
  422. to short or long integers is not appropriate.  A TIFF reader will generally
  423. need to store the field value as an uninterpreted byte sequence until it is
  424. fed to the JPEG decoder.
  425.  
  426. Multibyte quantities within the tables follow the ISO JPEG convention of
  427. MSB-first storage, regardless of the byte ordering of the surrounding TIFF
  428. file.
  429.  
  430. When the JPEGTables field is present, it shall contain a valid JPEG
  431. "abbreviated table specification" datastream.  This datastream shall begin
  432. with SOI and end with EOI.  It may contain zero or more JPEG "tables and
  433. miscellaneous" markers, namely:
  434.     DQT
  435.     DHT
  436.     DAC    (not to appear unless arithmetic coding is used)
  437.     DRI
  438.     APPn    (shall be ignored by TIFF readers)
  439.     COM    (shall be ignored by TIFF readers)
  440. Since JPEG defines the SOI marker to reset the DAC and DRI state, these two
  441. markers' values cannot be carried over into any image datastream, and thus
  442. they are effectively no-ops in the JPEGTables field.  To avoid confusion,
  443. it is recommended that writers not place DAC or DRI markers in JPEGTables.
  444. However readers must properly skip over them if they appear.
  445.  
  446. When JPEGTables is present, readers shall load the table specifications
  447. contained in JPEGTables before processing image segment datastreams.
  448. Image segments may simply refer to these preloaded tables without defining
  449. them.  An image segment can still define and use its own tables, subject to
  450. the restrictions below.
  451.  
  452. An image segment may not redefine any table defined in JPEGTables.  (This
  453. restriction is imposed to allow readers to process image segments in random
  454. order without having to reload JPEGTables between segments.)  Therefore, use
  455. of JPEGTables divides the available table slots into two groups: "global"
  456. slots are defined in JPEGTables and may be used but not redefined by
  457. segments; "local" slots are available for local definition and use in each
  458. segment.  To permit random access, a segment may not reference any local
  459. tables that it does not itself define.
  460.  
  461.  
  462. Special considerations for PlanarConfiguration 2
  463. ------------------------------------------------
  464.  
  465. In PlanarConfiguration 2, each image segment contains data for only one
  466. color component.  To avoid confusing the JPEG codec, we wish the segments
  467. to look like valid single-channel (i.e., grayscale) JPEG datastreams.  This
  468. means that different rules must be used for the SOFn parameters.
  469.  
  470. In PlanarConfiguration 2, the dimensions given in the SOFn of a subsampled
  471. component shall be scaled down by the sampling factors compared to the SOFn
  472. dimensions that would be used in PlanarConfiguration 1.  This is necessary
  473. to match the actual number of samples stored in that segment, so that the
  474. JPEG codec doesn't complain about too much or too little data.  In strip
  475. TIFF files the computed dimensions may need to be rounded up to the next
  476. integer; in tiled files, the restrictions on tile size make this case
  477. impossible.
  478.  
  479. Furthermore, all SOFn sampling factors shall be given as 1.  (This is
  480. merely to avoid confusion, since the sampling factors in a single-channel
  481. JPEG datastream have no real effect.)
  482.  
  483. Any downsampling will need to happen externally to the JPEG codec, since
  484. JPEG sampling factors are defined with reference to the full-precision
  485. component.  In PlanarConfiguration 2, the JPEG codec will be working on
  486. only one component at a time and thus will have no reference component to
  487. downsample against.
  488.  
  489.  
  490. Minimum requirements for TIFF/JPEG
  491. ----------------------------------
  492.  
  493. ISO JPEG is a large and complex standard; most implementations support only
  494. a subset of it.  Here we define a "core" subset of TIFF/JPEG which readers
  495. must support to claim TIFF/JPEG compatibility.  For maximum
  496. cross-application compatibility, we recommend that writers confine
  497. themselves to this subset unless there is very good reason to do otherwise.
  498.  
  499. Use the ISO baseline JPEG process: 8-bit data precision, Huffman coding,
  500. with no more than 2 DC and 2 AC Huffman tables.  Note that this implies
  501. BitsPerSample = 8 for each component.  We recommend deviating from baseline
  502. JPEG only if 12-bit data precision or lossless coding is required.
  503.  
  504. Use no subsampling (all JPEG sampling factors = 1) for color spaces other
  505. than YCbCr.  (This is, in fact, required with the TIFF 6.0 field
  506. definitions, but may not be so in future revisions.)  For YCbCr, use one of
  507. the following choices:
  508.     YCbCrSubSampling field        JPEG sampling factors
  509.     1,1                1h1v, 1h1v, 1h1v
  510.     2,1                2h1v, 1h1v, 1h1v
  511.     2,2  (default value)        2h2v, 1h1v, 1h1v
  512. We recommend that RGB source data be converted to YCbCr for best compression
  513. results.  Other source data colorspaces should probably be left alone.
  514. Minimal readers need not support JPEG images with colorspaces other than
  515. YCbCr and grayscale (PhotometricInterpretation = 6 or 1).
  516.  
  517. A minimal reader also need not support JPEG YCbCr images with nondefault
  518. values of YCbCrCoefficients or YCbCrPositioning, nor with values of
  519. ReferenceBlackWhite other than [0,255,128,255,128,255].  (These values
  520. correspond to the RGB<=>YCbCr conversion specified by JFIF, which is widely
  521. implemented in JPEG codecs.)
  522.  
  523. Writers are reminded that a ReferenceBlackWhite field *must* be included
  524. when PhotometricInterpretation is YCbCr, because the default
  525. ReferenceBlackWhite values are inappropriate for YCbCr.
  526.  
  527. If any subsampling is used, PlanarConfiguration=1 is preferred to avoid the
  528. possibly-confusing requirements of PlanarConfiguration=2.  In any case,
  529. readers are not required to support PlanarConfiguration=2.
  530.  
  531. If possible, use a single interleaved scan in each image segment.  This is
  532. not legal JPEG if there are more than 4 SamplesPerPixel or if the sampling
  533. factors are such that more than 10 blocks would be needed per MCU; in that
  534. case, use a separate scan for each component.  (The recommended color
  535. spaces and sampling factors will not run into that restriction, so a
  536. minimal reader need not support more than one scan per segment.)
  537.  
  538. To claim TIFF/JPEG compatibility, readers shall support multiple-strip TIFF
  539. files and the optional JPEGTables field; it is not acceptable to read only
  540. single-datastream files.  Support for tiled TIFF files is strongly
  541. recommended but not required.
  542.  
  543.  
  544. Other recommendations for implementors
  545. --------------------------------------
  546.  
  547. The TIFF tag Compression=7 guarantees only that the compressed data is
  548. represented as ISO JPEG datastreams.  Since JPEG is a large and evolving
  549. standard, readers should apply careful error checking to the JPEG markers
  550. to ensure that the compression process is within their capabilities.  In
  551. particular, to avoid being confused by future extensions to the JPEG
  552. standard, it is important to abort if unknown marker codes are seen.
  553.  
  554. The point of requiring that all image segments use the same JPEG process is
  555. to ensure that a reader need check only one segment to determine whether it
  556. can handle the image.  For example, consider a TIFF reader that has access
  557. to fast but restricted JPEG hardware, as well as a slower, more general
  558. software implementation.  It is desirable to check only one image segment
  559. to find out whether the fast hardware can be used.  Thus, writers should
  560. try to ensure that all segments of an image look as much "alike" as
  561. possible: there should be no variation in scan layout, use of options such
  562. as DRI, etc.  Ideally, segments will be processed identically except
  563. perhaps for using different local quantization or entropy-coding tables.
  564.  
  565. Writers should avoid including "noise" JPEG markers (COM and APPn markers).
  566. Standard TIFF fields provide a better way to transport any non-image data.
  567. Some JPEG codecs may change behavior if they see an APPn marker they
  568. think they understand; since the TIFF spec requires these markers to be
  569. ignored, this behavior is undesirable.
  570.  
  571. It is possible to convert an interchange-JPEG file (e.g., a JFIF file) to
  572. TIFF simply by dropping the interchange datastream into a single strip.
  573. (However, designers are reminded that the TIFF spec discourages huge
  574. strips; splitting the image is somewhat more work but may give better
  575. results.)  Conversion from TIFF to interchange JPEG is more complex.  A
  576. strip-based TIFF/JPEG file can be converted fairly easily if all strips use
  577. identical JPEG tables and no RSTn markers: just delete the overhead markers
  578. and insert RSTn markers between strips.  Converting tiled images is harder,
  579. since the data will usually not be in the right order (unless the tiles are
  580. only one MCU high).  This can still be done losslessly, but it will require
  581. undoing and redoing the entropy coding so that the DC coefficient
  582. differences can be updated.
  583.  
  584. There is no default value for JPEGTables: standard TIFF files must define all
  585. tables that they reference.  For some closed systems in which many files will
  586. have identical tables, it might make sense to define a default JPEGTables
  587. value to avoid actually storing the tables.  Or even better, invent a
  588. private field selecting one of N default JPEGTables settings, so as to allow
  589. for future expansion.  Either of these must be regarded as a private
  590. extension that will render the files unreadable by other applications.
  591.  
  592.  
  593. References
  594. ----------
  595.  
  596. [1] Wallace, Gregory K.  "The JPEG Still Picture Compression Standard",
  597. Communications of the ACM, April 1991 (vol. 34 no. 4), pp. 30-44.
  598.  
  599. This is the best short technical introduction to the JPEG algorithms.
  600. It is a good overview but does not provide sufficiently detailed
  601. information to write an implementation.
  602.  
  603. [2] Pennebaker, William B. and Mitchell, Joan L.  "JPEG Still Image Data
  604. Compression Standard", Van Nostrand Reinhold, 1993, ISBN 0-442-01272-1.
  605. 638pp.
  606.  
  607. This textbook is by far the most complete exposition of JPEG in existence.
  608. It includes the full text of the ISO JPEG standards (DIS 10918-1 and draft
  609. DIS 10918-2).  No would-be JPEG implementor should be without it.
  610.  
  611. [3] ISO/IEC IS 10918-1, "Digital Compression and Coding of Continuous-tone
  612. Still Images, Part 1: Requirements and guidelines", February 1994.
  613. ISO/IEC DIS 10918-2, "Digital Compression and Coding of Continuous-tone
  614. Still Images, Part 2: Compliance testing", final approval expected 1994.
  615.  
  616. These are the official standards documents.  Note that the Pennebaker and
  617. Mitchell textbook is likely to be cheaper and more useful than the official
  618. standards.
  619.  
  620.  
  621. Changes to Section 21: YCbCr Images
  622. ===================================
  623.  
  624. [This section of the Tech Note clarifies section 21 to make clear the
  625. interpretation of image dimensions in a subsampled image.  Furthermore,
  626. the section is changed to allow the original image dimensions not to be
  627. multiples of the sampling factors.  This change is necessary to support use
  628. of JPEG compression on odd-size images.]
  629.  
  630. Add the following paragraphs to the Section 21 introduction (p. 89),
  631. just after the paragraph beginning "When a Class Y image is subsampled":
  632.  
  633.     In a subsampled image, it is understood that all TIFF image
  634.     dimensions are measured in terms of the highest-resolution
  635.     (luminance) component.  In particular, ImageWidth, ImageLength,
  636.     RowsPerStrip, TileWidth, TileLength, XResolution, and YResolution
  637.     are measured in luminance samples.
  638.  
  639.     RowsPerStrip, TileWidth, and TileLength are constrained so that
  640.     there are an integral number of samples of each component in a
  641.     complete strip or tile.  However, ImageWidth/ImageLength are not
  642.     constrained.  If an odd-size image is to be converted to subsampled
  643.     format, the writer should pad the source data to a multiple of the
  644.     sampling factors by replication of the last column and/or row, then
  645.     downsample.  The number of luminance samples actually stored in the
  646.     file will be a multiple of the sampling factors.  Conversely,
  647.     readers must ignore any extra data (outside the specified image
  648.     dimensions) after upsampling.
  649.  
  650.     When PlanarConfiguration=2, each strip or tile covers the same
  651.     image area despite subsampling; that is, the total number of strips
  652.     or tiles in the image is the same for each component.  Therefore
  653.     strips or tiles of the subsampled components contain fewer samples
  654.     than strips or tiles of the luminance component.
  655.  
  656.     If there are extra samples per pixel (see field ExtraSamples),
  657.     these data channels have the same number of samples as the
  658.     luminance component.
  659.  
  660. Rewrite the YCbCrSubSampling field description (pp 91-92) as follows
  661. (largely to eliminate possibly-misleading references to
  662. ImageWidth/ImageLength of the subsampled components):
  663.  
  664.     (first paragraph unchanged)
  665.  
  666.     The two elements of this field are defined as follows:
  667.  
  668.     Short 0: ChromaSubsampleHoriz:
  669.  
  670.     1 = there are equal numbers of luma and chroma samples horizontally.
  671.  
  672.     2 = there are twice as many luma samples as chroma samples
  673.     horizontally.
  674.  
  675.     4 = there are four times as many luma samples as chroma samples
  676.     horizontally.
  677.  
  678.     Short 1: ChromaSubsampleVert:
  679.  
  680.     1 = there are equal numbers of luma and chroma samples vertically.
  681.  
  682.     2 = there are twice as many luma samples as chroma samples
  683.     vertically.
  684.  
  685.     4 = there are four times as many luma samples as chroma samples
  686.     vertically.
  687.  
  688.     ChromaSubsampleVert shall always be less than or equal to
  689.     ChromaSubsampleHoriz.  Note that Cb and Cr have the same sampling
  690.     ratios.
  691.  
  692.     In a strip TIFF file, RowsPerStrip is required to be an integer
  693.     multiple of ChromaSubSampleVert (unless RowsPerStrip >=
  694.     ImageLength, in which case its exact value is unimportant).
  695.     If ImageWidth and ImageLength are not multiples of
  696.     ChromaSubsampleHoriz and ChromaSubsampleVert respectively, then the
  697.     source data shall be padded to the next integer multiple of these
  698.     values before downsampling.
  699.  
  700.     In a tiled TIFF file, TileWidth must be an integer multiple of
  701.     ChromaSubsampleHoriz and TileLength must be an integer multiple of
  702.     ChromaSubsampleVert.  Padding will occur to tile boundaries.
  703.  
  704.     The default values of this field are [ 2,2 ].  Thus, YCbCr data is
  705.     downsampled by default!
  706.